package org.directfire.player;

import org.directfire.player.model.Model;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;

/**
 *   Copyright (C) 2012 Alexey Kostrjitskiy mailto:alex@ipss.net
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 * $Id: VolumeControlReceiver.java 22 2012-08-27 06:00:54Z alexeyvk@gmail.com $<br/>
 * $HeadURL: http://directfire.googlecode.com/svn/branches/0.1/DirectFireAndroid/src/org/directfire/player/VolumeControlReceiver.java $<br/><br/>
 *
 * DirectFirePlayer broadcast receiver for media buttons.
 */
public class VolumeControlReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		//Log.w("BROADCAST", ""+intent.toString());
  	if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
  		KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
			if(event.getAction() == KeyEvent.ACTION_UP){
	  		switch(event.getKeyCode()){
	  		 case KeyEvent.KEYCODE_VOLUME_DOWN:
	  			 Log.w("ACTION_MEDIA_BUTTON", ""+event.toString());//TODO: WTF where it is???
	  			 break;
	  		 case KeyEvent.KEYCODE_VOLUME_UP:
	  			 Log.w("ACTION_MEDIA_BUTTON", ""+event.toString());//TODO: WTF where it is???
	  			break;
	  		 case KeyEvent.KEYCODE_MEDIA_PLAY:
	  		 case KeyEvent.KEYCODE_MEDIA_PAUSE:
	  		 case KeyEvent.KEYCODE_MEDIA_STOP:
	  			 Log.w("ACTION_MEDIA_BUTTON", ""+event.toString());	  			 
	  			break;
	  		 case KeyEvent.KEYCODE_HEADSETHOOK:
 					try {
 						if(Model.getI().getmService()!=null){
 							if(!Model.getI().isPlaying()){
 								Bundle b = new Bundle();
 								b.putInt("station", 0);
 								Message msg=Message.obtain(null, DirectFireService.DFS_PLAY);
 								msg.setData(b);
	  						Model.getI().getmService().send(msg);
 							}else{
 								Message msg=Message.obtain(null, DirectFireService.DFS_STOP);
	  						Model.getI().getmService().send(msg);
 							}
 						}
					} catch (RemoteException e) {
						throw Model.getI().exception(e, 402);
					}
	  			break;
	  		 default:
	  		}
			}
  	}
  }
}
